package com.example.mailserver.service;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author zhouyp
 * @program example-parent
 * @description
 * @create 2020-08-03
 */
@Service
@Component
public class EmailService implements com.example.service.EmailService {

	@Autowired
	private JavaMailSender mailSender;

	@Value("${spring.mail.username}")
	private String from;

	/**
	 * 发送验证码到邮箱，true成功，false失败
	 *
	 * @return boolean
	 */
	public boolean sendEmail(String email, String code) {
		System.out.println("准备发送邮件：" + email + "；验证码：" + code);
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from); // 邮件发送者
		message.setTo(email); // 邮件接受者
		message.setSubject("example-mail-service-注册验证码"); // 主题
		message.setText("验证码：" + code); // 内容

		mailSender.send(message);
		return true;
	}
}
